package com.ruoyi.common.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.type.WritableTypeId;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.ruoyi.common.annotation.UserMemberInfo;
import com.ruoyi.common.core.domain.model.UserBusinessInfo;
import com.ruoyi.common.core.service.UserMemberBusinessService;
import com.ruoyi.common.utils.spring.SpringUtils;
import lombok.NoArgsConstructor;

import java.io.IOException;

/**
 * Description:
 * Author: YangShiWei
 * Email: strongwalter2014@gmail.com
 * Datetime: 2021/5/7 17:43
 */
@NoArgsConstructor
public class UserMemberInfoSerializer extends JsonSerializer<Long> implements ContextualSerializer {

    private boolean onlyName;

    public UserMemberInfoSerializer(boolean onlyName) {
        this.onlyName = onlyName;
    }

    @Override
    public void serialize(Long value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (value != null) {
            UserMemberBusinessService userMemberBusinessService = SpringUtils.getBean(UserMemberBusinessService.class);
            UserBusinessInfo userBusinessInfo = userMemberBusinessService.queryById(value);
            if (userBusinessInfo != null) {
                if (onlyName) {
                    jsonGenerator.writeString(userBusinessInfo.getName());
                } else {
                    jsonGenerator.writeObject(userBusinessInfo);
                }
            } else {
                jsonGenerator.writeObject(value);
            }
        } else {
            jsonGenerator.writeObject(null);
        }
    }

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
        if (beanProperty != null) { // 为空直接跳过
            UserMemberInfo userMemberInfo = beanProperty.getAnnotation(UserMemberInfo.class);
            if (userMemberInfo == null) {
                userMemberInfo = beanProperty.getContextAnnotation(UserMemberInfo.class);
            }
            if (userMemberInfo != null) {
                return new UserMemberInfoSerializer(userMemberInfo.onlyName());
            }
            return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty);
        }
        return serializerProvider.findNullValueSerializer(null);
    }

    @Override
    public void serializeWithType(Long value, JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
        WritableTypeId typeIdDef = typeSer.writeTypePrefix(gen, typeSer.typeId(value, JsonToken.VALUE_STRING));
        serialize(value, gen, serializers);
        typeSer.writeTypeSuffix(gen, typeIdDef);
    }

}
